<template>
  <div class="md-tooltip" @mouseenter="showTooltip" @mouseleave="hideTooltip">
    <div class="md-tooltip__trigger">
      <slot></slot>
    </div>
    <transition name="md-tooltip">
      <div v-show="visible" class="md-tooltip__content" :class="positionClass">
        {{ content }}
      </div>
    </transition>
  </div>
</template>

<script setup lang="ts">
import { ref, defineProps, computed } from 'vue';

interface Props {
  content: string;
  position?: 'top' | 'bottom' | 'left' | 'right';
  delay?: number;
}

const props = withDefaults(defineProps<Props>(), {
  position: 'top',
  delay: 200
});

const visible = ref(false);
let timer: ReturnType<typeof setTimeout> | null = null;

const positionClass = computed(() => `md-tooltip__content--${props.position}`);

const showTooltip = () => {
  if (timer) clearTimeout(timer);
  timer = setTimeout(() => {
    visible.value = true;
  }, props.delay);
};

const hideTooltip = () => {
  if (timer) clearTimeout(timer);
  visible.value = false;
};
</script>

<style lang="scss" scoped>
.md-tooltip {
  position: relative;
  display: inline-block;
  
  &__content {
    position: absolute;
    z-index: 1001;
    padding: 6px 10px;
    font-size: 12px;
    line-height: 1.5;
    background-color: rgba(0, 0, 0, 0.75);
    color: #ffffff;
    border-radius: 4px;
    white-space: nowrap;
    
    &--top {
      bottom: 100%;
      left: 50%;
      transform: translateX(-50%) translateY(-8px);
      margin-bottom: 8px;
      
      &::after {
        content: '';
        position: absolute;
        top: 100%;
        left: 50%;
        margin-left: -5px;
        border-width: 5px;
        border-style: solid;
        border-color: rgba(0, 0, 0, 0.75) transparent transparent transparent;
      }
    }
    
    &--bottom {
      top: 100%;
      left: 50%;
      transform: translateX(-50%) translateY(8px);
      margin-top: 8px;
      
      &::after {
        content: '';
        position: absolute;
        bottom: 100%;
        left: 50%;
        margin-left: -5px;
        border-width: 5px;
        border-style: solid;
        border-color: transparent transparent rgba(0, 0, 0, 0.75) transparent;
      }
    }
    
    &--left {
      right: 100%;
      top: 50%;
      transform: translateY(-50%) translateX(-8px);
      margin-right: 8px;
      
      &::after {
        content: '';
        position: absolute;
        top: 50%;
        left: 100%;
        margin-top: -5px;
        border-width: 5px;
        border-style: solid;
        border-color: transparent transparent transparent rgba(0, 0, 0, 0.75);
      }
    }
    
    &--right {
      left: 100%;
      top: 50%;
      transform: translateY(-50%) translateX(8px);
      margin-left: 8px;
      
      &::after {
        content: '';
        position: absolute;
        top: 50%;
        right: 100%;
        margin-top: -5px;
        border-width: 5px;
        border-style: solid;
        border-color: transparent rgba(0, 0, 0, 0.75) transparent transparent;
      }
    }
  }
}

.md-tooltip-enter-active,
.md-tooltip-leave-active {
  transition: opacity 0.3s, transform 0.3s;
}

.md-tooltip-enter-from,
.md-tooltip-leave-to {
  opacity: 0;
  transform: translateX(-50%) translateY(0);
}
</style>